<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go gRPC研究总结 | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go gRPC研究总结</h1>
<h3 id="什么是rpc">什么是RPC</h3>
<p>RPC（Remote Procedure Call）—远程过程调用，它是一种通过网络从远程计算机程序上请求服务，而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在，如TCP或UDP，为通信程序之间携带信息数据。在OSI网络通信模型中，RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。——百度百科</p>
<p>支持环境配置(Mac)</p>
<p>安装protobuf 这是 Google 开源的一套成熟的结构数据序列化机制<a href="https://juejin.im/post/5b852d476fb9a019e4505873">protocol buffer</a>
安装protobuf 这是 Google 开源的一套成熟的结构数据序列化机制<a href="https://juejin.im/post/5b852d476fb9a019e4505873">Protocol Buffers 3 简明教程</a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-protobuf" data-lang="protobuf"><span style="display:flex;"><span>brew info protobuf<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>brew install protobuf<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;<span style="color:#f92672">/</span>code&gt;&lt;<span style="color:#f92672">/</span>pre&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">检验</span>protobuf安装结果<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;pre&gt;&lt;code class<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;language-shell line-numbers&#34;</span>&gt;protoc <span style="color:#f92672">--</span>version<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>libprotoc <span style="color:#ae81ff">3.5.1</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;<span style="color:#f92672">/</span>code&gt;&lt;<span style="color:#f92672">/</span>pre&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">安装第三方包
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;pre&gt;&lt;code class<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;language-go line-numbers&#34;</span>&gt;go mod download github.com<span style="color:#f92672">/</span>golang<span style="color:#f92672">/</span>protobuf<span style="color:#f92672">/</span>proto<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>go mod download github.com<span style="color:#f92672">/</span>golang<span style="color:#f92672">/</span>protobuf<span style="color:#f92672">/</span>protoc<span style="color:#f92672">-</span>gen<span style="color:#f92672">-</span>go<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>go mod download google.golang.org<span style="color:#f92672">/</span>grpc<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>go install github.com<span style="color:#f92672">/</span>golang<span style="color:#f92672">/</span>protobuf<span style="color:#f92672">/</span>protoc<span style="color:#f92672">-</span>gen<span style="color:#f92672">-</span>go <span style="color:#75715e">//编译 protoc-gen-go 可执行文件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>&lt;<span style="color:#f92672">/</span>code&gt;&lt;<span style="color:#f92672">/</span>pre&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">创建</span> protobuf <span style="color:#960050;background-color:#1e0010">文件
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;pre&gt;&lt;code class<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;language-shell line-numbers&#34;</span>&gt;vi add.proto<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>add some date<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;<span style="color:#f92672">/</span>code&gt;&lt;<span style="color:#f92672">/</span>pre&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">生成</span> gRPC <span style="color:#960050;background-color:#1e0010">代码
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;pre&gt;&lt;code class<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;language-shell line-numbers&#34;</span>&gt;protoc <span style="color:#f92672">-</span>I <span style="color:#f92672">./</span>protos <span style="color:#f92672">./</span>protos<span style="color:#f92672">/</span>helloworld.proto <span style="color:#f92672">--</span>go_out<span style="color:#f92672">=</span>plugins<span style="color:#f92672">=</span>grpc<span style="color:#f92672">:</span>helloworld<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">或
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>protoc <span style="color:#f92672">-</span>I <span style="color:#f92672">.</span> add.proto <span style="color:#f92672">--</span>go_out<span style="color:#f92672">=</span>plugins<span style="color:#f92672">=</span>grpc<span style="color:#f92672">:.</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>&lt;<span style="color:#f92672">/</span>code&gt;&lt;<span style="color:#f92672">/</span>pre&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">这生成了</span> <span style="color:#960050;background-color:#1e0010">`</span>helloworld.pb.go<span style="color:#960050;background-color:#1e0010">`</span> <span style="color:#960050;background-color:#1e0010">，包含了我们生成的客户端和服务端类，此外还有用于填充、序列化、提取</span> <span style="color:#960050;background-color:#1e0010">`</span>HelloRequest<span style="color:#960050;background-color:#1e0010">`</span> <span style="color:#960050;background-color:#1e0010">和</span> <span style="color:#960050;background-color:#1e0010">`</span>HelloResponse<span style="color:#960050;background-color:#1e0010">`</span> <span style="color:#960050;background-color:#1e0010">消息类型的类。
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>  <span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">在</span>server.go <span style="color:#960050;background-color:#1e0010">实现</span>AddServiceServer <span style="color:#960050;background-color:#1e0010">的接口方法
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">```</span>go<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>type HelloServer struct {<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>}<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>func (h HelloServer) SayHello(c context.Context, in <span style="color:#960050;background-color:#1e0010">*</span>pb.HelloRequest) (<span style="color:#960050;background-color:#1e0010">*</span>pb.HelloReply, error) {<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	return <span style="color:#960050;background-color:#1e0010">&amp;</span>pb.HelloReply{Message<span style="color:#f92672">:</span> <span style="color:#e6db74">&#34;hello &#34;</span> <span style="color:#f92672">+</span> in.Name}, nil<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>}	<span style="color:#960050;background-color:#1e0010">
</span></span></span></code></pre></div><p>打开两个终端，分别启动 serve &amp; client</p>
<p>浏览器访问输出返回结果。</p>

    </article>
  </div>

  
  
  

  
</body>

</html>